package com.doge.calcalbum.business.home

import androidx.fragment.app.FragmentActivity
import com.doge.calcalbum.api.*
import com.doge.calcalbum.api.model.WechatChannelInfo

object WxServiceHelper {
    private var channel: WechatChannelInfo? = null

    fun show(activity: FragmentActivity?) {
        if (activity == null) return
        requireImage(activity) {
            WechatPublicChannelDialog.Builder(activity)
                .setChannel(it)
                .create().show()
        }
    }


    private fun requireImage(activity: FragmentActivity, callback:(WechatChannelInfo)->Unit) {
        if (channel != null) {
            channel?.let(callback)
        } else {
            api.wechatPublicChannel()
                .map(ApiResultMapTransformer())
                .subscribeOnIO()
                .observeOnMain()
                .subscribe(object : SimpleObserverAdapter<WechatChannelInfo>(activity, activity) {
                    override fun onNext(t: WechatChannelInfo) {
                        channel = t
                        callback(t)
                    }
                })
        }
    }
}